package com.eleadmin.order.biz.impl;

import com.eleadmin.damon.utils.constant.UserConstant;
import com.eleadmin.damon.utils.results.BaseDao;
import com.eleadmin.damon.utils.results.PageResult2;

import com.eleadmin.damon.utils.utils.Utils;
import com.eleadmin.common.system.entity.User;
import com.eleadmin.order.biz.EventsBiz;
import com.eleadmin.order.entity.Events;
import com.eleadmin.common.core.web.JsonResult;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.Map;
import java.util.List;

/**
 * 数据层实现类
 *
 * @author : Doman
 * @version ：1.0
 * @date : 2023-08-01 17:08:54
 */
@Repository
public class EventsBizImpl extends BaseDao implements EventsBiz {

    @Override
    public JsonResult list(PageResult2<T> page, Events events, User loginUser) throws Exception {
        Map<String, String> map = new HashMap<String, String>();
        RowMapper<Events> rm = new BeanPropertyRowMapper<Events>(Events.class);
        StringBuffer sql = new StringBuffer("select e.id,e.organization_id,e.event_date");
        sql.append(",e.customer_id,e.customer,e.content,e.remark");
        sql.append(",e.type,e.create_time,e.create_user,e.update_time");
        sql.append(",e.update_user");
        sql.append(",sogzn.organization_name organizationName,suser.nickname createName,suser2.nickname updateName ");
        sql.append(" from chf_events e ");
        sql.append(" left join sys_user suser on suser.user_id=e.create_user ");
        sql.append(" left join sys_user suser2 on suser2.user_id=e.update_user ");
        sql.append(" inner join sys_organization sogzn on sogzn.organization_id=e.organization_id and sogzn.deleted=0 ");
        sql.append(" where e.deleted=0 ");
        if (loginUser.getUserId() != UserConstant.ADMIN_ID) {
            sql.append(" and e.organization_id=:tableOrganizationId ");
            map.put("tableOrganizationId", loginUser.getOrganizationId() + "");
        }
        if (Utils.isNotEmpty(events)) {
            if (Utils.isNotEmpty(events.getCustomer())) {
                sql.append(" and instr(e.customer,:customer)>0 ");
                map.put("customer", events.getCustomer());
            }
            if (Utils.isNotEmpty(events.getEventDate())) {
                sql.append(" and instr(e.event_date,:event_date)>0 ");
                map.put("event_date", events.getEventDate());
            }
            if (Utils.isNotEmpty(events.getSort()) && Utils.isNotEmpty(events.getOrder())) {
                sql.append(" order by e." + events.getSort() + " " + events.getOrder());
            } else {
                sql.append(" order by e.create_time desc");
            }
        }
        return jsonResult(getPageQueryList(page, sql.toString(), map, rm));
    }

    @Override
    public List<Events> list(Events events, User loginUser) {
        Map<String, String> map = new HashMap<String, String>();
        RowMapper<Events> rm = new BeanPropertyRowMapper<Events>(Events.class);
        StringBuffer sql = new StringBuffer("select e.id,e.organization_id,e.event_date");
        sql.append(",e.customer_id,e.customer,e.content,e.remark");
        sql.append(",e.type,e.create_time,e.create_user,e.update_time");
        sql.append(",e.update_user");
        sql.append(",sogzn.organization_name organizationName,suser.nickname createName ");
        sql.append(" from chf_events e ");
        sql.append(" inner join sys_user suser on suser.user_id=e.create_user ");
        sql.append(" inner join sys_organization sogzn on sogzn.organization_id=e.organization_id and sogzn.deleted=0 ");
        sql.append(" where e.deleted=0 ");
        if (loginUser.getUserId() != UserConstant.ADMIN_ID) {
            sql.append(" and e.organization_id=:tableOrganizationId ");
            map.put("tableOrganizationId", loginUser.getOrganizationId() + "");
        }
        if (Utils.isNotEmpty(events)) {
            if (Utils.isNotEmpty(events.getCustomer())) {
                sql.append(" and instr(e.customer,:customer)>0 ");
                map.put("customer", events.getCustomer());
            }
            if (Utils.isNotEmpty(events.getEventDate())) {
                sql.append(" and instr(e.event_date,:event_date)>0 ");
                map.put("event_date", events.getEventDate());
            }
            if (Utils.isNotEmpty(events.getSort()) && Utils.isNotEmpty(events.getOrder())) {
                sql.append(" order by e." + events.getSort() + " " + events.getOrder());
            } else {
                sql.append(" order by e.create_time desc");
            }
            if(Utils.isNotEmpty(events.getChecked())){
                sql.append(" limit "+events.getChecked());
            }
        }
        getMapParameter(sql, map);
        return jdbcTemplate.query(sql.toString(), map, rm);
    }

    @Override
    public Events getById(String id) {
        RowMapper<Events> rm = new BeanPropertyRowMapper<Events>(Events.class);
        Map<String, String> map = new HashMap<String, String>();
        StringBuffer sql = new StringBuffer("select e.id,e.organization_id,e.event_date");
        sql.append(",e.customer_id,e.customer,e.content,e.remark");
        sql.append(",e.type,e.create_time,e.create_user,e.update_time");
        sql.append(",e.update_user");
        sql.append(",sogzn.organization_name organizationName,suser.nickname createName ");
        sql.append(" from chf_events e ");
        sql.append(" inner join sys_user suser on suser.user_id=e.create_user ");
        sql.append(" inner join sys_organization sogzn on sogzn.organization_id=e.organization_id and sogzn.deleted=0 ");
        sql.append(" where e.deleted=0 and e.id=:id ");
        map.put("id", id);
        getMapParameter(sql, map);
        return jdbcTemplate.queryForObject(sql.toString(), map, rm);
    }

    @Override
    public boolean save(Events events, User loginUser) {
        Map<String, String> map = new HashMap<String, String>();
        StringBuffer sql = new StringBuffer("insert into chf_events (");
        sql.append("organization_id,event_date,customer_id,customer");
        sql.append(",content,remark,type,create_user,create_time) values ( ");
        sql.append(":organization_id,:event_date,:customer_id,:customer");
        sql.append(",:content,:remark,:type,:create_user,now())");
        map.put("organization_id", loginUser.getOrganizationId() + "");
        map.put("event_date", events.getEventDate());
        map.put("customer_id", events.getCustomerId());
        map.put("customer", events.getCustomer());
        map.put("content", events.getContent());
        map.put("remark", events.getRemark());
        map.put("type", events.getType());
        map.put("create_user", loginUser.getUserId() + "");
        getMapParameter(sql, map);
        int count = jdbcTemplate.update(sql.toString(), map);
        return count > 0;
    }

    @Override
    public boolean update(Events events, User loginUser) {
        Map<String, String> map = new HashMap<String, String>();
        StringBuffer sql = new StringBuffer("update chf_events set ");
        sql.append("event_date=:event_date,customer_id=:customer_id,customer=:customer ");
        sql.append(",content=:content,remark=:remark,type=:type ");
        sql.append(",update_time=now(),update_user=:update_user where id=:id ");
        map.put("event_date", events.getEventDate());
        map.put("customer_id", events.getCustomerId());
        map.put("customer", events.getCustomer());
        map.put("content", events.getContent());
        map.put("remark", events.getRemark());
        map.put("type", events.getType());
        map.put("update_user", loginUser.getUserId() + "");
        map.put("id", events.getId());
        getMapParameter(sql, map);
        int count = jdbcTemplate.update(sql.toString(), map);
        return count > 0;
    }

    @Override
    public boolean remove(String id, User loginUser) {
        int count = 0;
        Map<String, String> map = null;
        if (id.length() > 0) {
            for (String id1 : id.split(",")) {
                map = new HashMap<String, String>();
                if (id1 == null || id1.equals("")) {
                    break;
                }
                StringBuffer sql = new StringBuffer("update chf_events set deleted=1" +
                        ",update_user=:update_user, update_time=now() "
                        + " where id =:id ");
                map.put("update_user", loginUser.getUserId() + "");
                map.put("id", id1);
                getMapParameter(sql, map);
                count = jdbcTemplate.update(sql.toString(), map);
            }
        }
        return count > 0;
    }
}
